package com.ztgf.order.service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import com.ztgf.order.common.config.YmlConfig;
import com.ztgf.order.mapper.OrderPayCallBackInfoMapper;
import com.ztgf.order.models.bo.OrderPayBO;
import com.ztgf.order.models.bo.OrderPayCallBackInfoSaveBO;
import com.ztgf.order.models.dto.OrderInfoDTO;
import com.ztgf.order.models.dto.OrderPayCallBackInfoDTO;
import com.ztgf.order.models.service.IOrderInfoService;
import com.ztgf.order.models.service.IOrderPayCallBackInfoService;
import com.ztgf.order.models.service.IOrderPayRelationService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;

import javax.annotation.Resource;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 支付回调信息表控制器
 *
 * @author YUE
 * @Date 2020-11-12 10:12:39
 */
@DubboService
@Lazy
public class OrderPayCallBackInfoServiceImpl extends ServiceImpl<OrderPayCallBackInfoMapper, OrderPayCallBackInfoDTO> implements IOrderPayCallBackInfoService{

    @Resource
    private YmlConfig ymlConfig;

    @Autowired
    private IOrderInfoService orderInfoService;

    private Lock lock = new ReentrantLock();

    @Override
    public boolean add(OrderPayCallBackInfoSaveBO saveBO) {
        OrderPayCallBackInfoDTO dto = new OrderPayCallBackInfoDTO();
        BeanUtils.copyProperties(saveBO,dto);
        Long id = ymlConfig.getPkId();
        dto.setId(id);
        return this.insert(dto);
    }

    public boolean addByOrderPayBO(OrderPayBO orderPayBO) {
        OrderPayCallBackInfoDTO dto = new OrderPayCallBackInfoDTO();
        BeanUtils.copyProperties(orderPayBO,dto);
        Long id = ymlConfig.getPkId();
        dto.setId(id);
        return this.insert(dto);
    }

    public boolean isExistCommissionInfoByOrderId(Long orderId){
        OrderPayCallBackInfoDTO commissionInfoByOrderId = getCommissionInfoByOrderId(orderId);
        if(null == commissionInfoByOrderId)
            return false;
        return true;
    }

    /**
     * 根据订单id获取支付佣金信息
     * @param orderId
     * @return
     */
    public OrderPayCallBackInfoDTO getCommissionInfoByOrderId(Long orderId){
        Wrapper wrapper = new EntityWrapper();
        wrapper.eq("order_id", orderId);
        wrapper.eq("type", 3);
        return selectOne(wrapper);
    }

    /**
     * 手动支付佣金
     * @param orderId
     * @return
     */
    @Override
    public boolean payCommission(Long orderId){
        lock.lock();
        Result result = new Result(CodeEnum.FAIL);
        try {
            OrderPayCallBackInfoDTO commissionInfoByOrderId = getCommissionInfoByOrderId(orderId);
            if(commissionInfoByOrderId.getPayStatus() != 3){//不是失败，不让进行
                return false;
            }
            OrderInfoDTO orderInfoDTO = orderInfoService.selectById(orderId);
//            result = orderPayRelationService.payCommission(orderInfoDTO.getLastCommissionMoney(), orderInfoDTO);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            lock.unlock();
        }
        return result.getCode() == CodeEnum.SUCCESS.getCode();
    }
}
